﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class Admin_PhanQuyen : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;
            NhiemVuQT();
            lbKQ.Text = "";
        }
    }
    PhanQuyenCtr pq = new PhanQuyenCtr();
    public void NhiemVuQT()
    {

        string sfile = Server.MapPath("~/dataQTV.txt");
        using (System.IO.StreamReader reader = new System.IO.StreamReader(sfile))
        {
            string snoidung = reader.ReadToEnd();

            string[] tem = snoidung.Split(new Char[] { '\r', '\n' });
            for (int i = 0; i < 17;i++ )
            {
                if (bool.Parse(tem[i*2])==true)
                {
                    cblistQuanTriVien.Items[i].Selected = true;
                }
                else
                {
                    cblistQuanTriVien.Items[i].Selected = false;
                }
            }

        }
    }
    public void NhiemVuNV()
    {

        string sfile = Server.MapPath("~/dataNV.txt");
        using (System.IO.StreamReader reader = new System.IO.StreamReader(sfile))
        {
            string snoidung = reader.ReadToEnd();

            string[] tem = snoidung.Split(new Char[] { '\r', '\n' });
            for (int i = 0; i < 17; i++)
            {
                if (bool.Parse(tem[i * 2]) == true)
                {
                    cblistNhanVien.Items[i].Selected = true;
                }
                else
                {
                    cblistNhanVien.Items[i].Selected = false;
                }
            }
            

        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        switch (rdlsanpham.SelectedIndex)
        {
            case 0:
                //Luu noi dung moi vao file
                //tao file theo duong dan cua website
                string sfile = Server.MapPath("~/dataQTV.txt");
                //mo file neu khac null thi bat dau them
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(sfile))
                {
                    foreach (ListItem i in cblistQuanTriVien.Items)
                    {
                        if (i.Selected)
                            writer.WriteLine(true);
                        else
                            writer.WriteLine(false);
                    }
                    //Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Phân quyền quản trị thành công.');window.location=\"AdminManager.aspx\"</script>");
                    writer.Close();
                    
                }
                //Response.Write("AdminManager.aspx");
                lbKQ.Text = "Phân quyền quản trị thành công.";
                break;
                 //Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Phân quyền quản trị thành công.');window.location=\"AdminManager.aspx\"</script>");
            case 1:
                //Luu noi dung moi vao file
                //tao file theo duong dan cua website
                sfile = Server.MapPath("~/dataNV.txt");
                //mo file neu khac null thi bat dau them
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(sfile))
                {
                    foreach (ListItem i in cblistNhanVien.Items)
                    {
                        if (i.Selected)
                            writer.WriteLine(true);
                        else
                            writer.WriteLine(false);
                    }

                    writer.Close();
                }
                lbKQ.Text = "Phân quyền nhân viên thành công.";
                break;
        }
        //Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Phân quyền nhân viên thành công.');window.location=\"AdminManager.aspx\"</script>");
    }
    protected void rdlsanpham_SelectedIndexChanged(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = rdlsanpham.SelectedIndex;
        if (rdlsanpham.SelectedIndex == 0)
        {
            NhiemVuQT();
            lbKQ.Text = "";
        }
        else
        {
            NhiemVuNV();
            lbKQ.Text = "";
        }
    }
}